<?php

set_time_limit(0);

require_once(dirname(__FILE__) . '/../config/saasapi.php');

define('SASS_APP_KEY', 'taoguan');
define('SAAS_SECRE_KEY', '49F4589687E79D815339B13A73E5FBB4');

function fetchHostListByCode($code) {

    $api = new SaasOpenClient();
    $api->appkey = SASS_APP_KEY;
    $api->secretKey = SAAS_SECRE_KEY;
    $api->format = 'json';

    $params = array('service_code' => $code);
    $result = $api->execute('host.getlist', $params);

    unset($api);
    if ($result->success == 'true') {
        if ($result->data == 'QUEUE_END') {
            return null;
        } else {
            return $result->data;
        }
    } else {
        return null;
    }
}

function getInfoByHost($host) {

    $api = new SaasOpenClient();
    $api->appkey = SASS_APP_KEY;
    $api->secretKey = SAAS_SECRE_KEY;
    $api->format = 'json';

    $params = array('server_name' => $host);
    $result = $api->execute('host.getinfo_byservername', $params);

    unset($api);
    if ($result->success == 'true') {
        if ($result->data == 'QUEUE_END') {
            return null;
        } else {
            return $result->data;
        }
    } else {
        return null;
    }
}

function callUpdateProcess($host) {
 
    $info = getInfoByHost($host);
    if ($info->status != 'HOST_STATUS_DELETED') {
        
        $serverName = $host;
		$orderId = $info->order_id;
		$hostId = $info->host_id;
	
		$cmd = sprintf("/usr/local/php/bin/php /data/httpd/tg.taoex.com/script/updateDomain.php %s %s %s", $serverName, $orderId, $hostId);
	
		exec($cmd ,$b, $a);
		echo $host."\n";
    }
}


$finfo = fetchHostListByCode('taoex-tg');

$info = array_merge((array) $finfo->host_name_list);
foreach ((array) $info as $host) {

    callUpdateProcess($host);
}
